package com.homelock.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.homelock.common.util.CGlibMapper;
import com.homelock.entity.HlCommunityAdv;
import com.homelock.entity.HlDevUser;
import com.homelock.vo.CommunityAdVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.homelock.entity.HlDevInfo;
import com.homelock.vo.DevCommunityVo;
import com.pi.base.exception.ServiceException;
import com.pi.community.dao.entity.BaseCommunityEntity;
import com.pi.community.service.CommunityService;
import com.pi.community.vo.CommunityVo;
import org.springframework.util.CollectionUtils;

@Service
public class UserCommunityService {

    private static final Logger logger = LoggerFactory.getLogger(UserCommunityService.class);

    private CommunityService communityService;

    private HlDevInfoService devInfoService;

    private LockUserService lockUserService;
    private HlCommunityAdvService hlCommunityAdvService;

    @Autowired
    public UserCommunityService(CommunityService communityService, HlDevInfoService devInfoService
    ,LockUserService lockUserService,HlCommunityAdvService hlCommunityAdvService) {
        Assert.notNull(communityService, "名称索引基础服务为空");
        this.communityService = communityService;
        this.devInfoService = devInfoService;
        this.lockUserService = lockUserService;
        this.hlCommunityAdvService = hlCommunityAdvService;
    }

    public List<CommunityVo> queryByName(String name) {
        return communityService.searchCommunity(name);
    }

    public Long addCommunity(String name, String address) {
        return communityService.addCommunity(name, address);
    }

    public void bindCommunity(Long userId, Long devId, Long communityId) {
        BaseCommunityEntity entity = communityService.findOne(communityId);
        if (null == entity) {
            throw new ServiceException("小区不存在，无法绑定");
        }
        HlDevInfo devInfo = devInfoService.getDevInfobyId(devId);
        if (null == devInfo || !devInfo.getState()) {
            throw new ServiceException("门锁已注销");
        }
        // 查询当前是否有绑定
        if (devInfo.getCommunityId().longValue() == communityId) {
            return;
        }
        if (devInfo.getCommunityId() != 0) {
            throw new ServiceException("已绑定小区, 不能重复绑定");
        }
        devInfoService.updateDevCommunity(devId, communityId);
    }

    public void unBindCommunity(Long userId, Long devId, Long communityId) {
        BaseCommunityEntity entity = communityService.findOne(communityId);
        if (null == entity) {
            throw new ServiceException("小区不存在，无法绑定");
        }
        HlDevInfo devInfo = devInfoService.getDevInfobyId(devId);
        if (null == devInfo || !devInfo.getState()) {
            throw new ServiceException("门锁已注销");
        }
        if(devInfo.getCommunityId().longValue() != communityId) {
            throw new ServiceException("未绑定该小区，无法注销");
        }
        devInfoService.updateDevCommunity(devId, 0L);
    }

    public BaseCommunityEntity queryByDevId(Long devId) {
        if (null == devId) {
            logger.error("[user_community_query], 设备编号为空 {}", devId);
            return null;
        }
        HlDevInfo devInfo = devInfoService.getDevInfobyId(devId);
        if (null == devInfo || 0 == devInfo.getCommunityId()) {
            return null;
        }
        BaseCommunityEntity entity = communityService.findOne(devInfo.getCommunityId());
        return entity;
    }

    public DevCommunityVo listDevCommunity(Long devId) {
        BaseCommunityEntity communityEntity = queryByDevId(devId);
        if(null == communityEntity) {
            return null;
        }
        DevCommunityVo vo = new DevCommunityVo();
        vo.setCommunityAddress(communityEntity.getCommunityAddress());
        vo.setCommunityName(communityEntity.getCommunityName());
        vo.setId(communityEntity.getId());
        return vo;
    }
    
    public DevCommunityVo listDevCommunityDetail(Long devId) {
        BaseCommunityEntity communityEntity = queryByDevId(devId);
        if(null == communityEntity) {
            return null;
        }
        DevCommunityVo vo = new DevCommunityVo();
        vo.setCommunityAddress(communityEntity.getCommunityAddress());
        vo.setCommunityName(communityEntity.getCommunityName());
        vo.setId(communityEntity.getId());
        vo.setContact(communityEntity.getContact());
        return vo;
    }

    public List<CommunityAdVo> queryAdList(Long userId){
        // 查询用户下的设备数
        List<CommunityAdVo> list = new ArrayList<>();
        List<HlDevInfo> devInfoList = devInfoService.getDevInfoAndCommunityNotNull(userId);
        // 查询设备的物业信息
        if(CollectionUtils.isEmpty(devInfoList)){
            return list;
        }
      // 只弹出第一个物业 的广告
        Long communityId = devInfoList.get(0).getCommunityId();
        List<HlCommunityAdv> advList = hlCommunityAdvService.queryListByCommunityId(communityId);
        if(CollectionUtils.isEmpty(advList)){
            return list;
        }
        for(HlCommunityAdv adv:advList){
            CommunityAdVo communityAdVo = CGlibMapper.mapper(adv,CommunityAdVo.class);
            if(communityAdVo != null){
                communityAdVo.setCreateTime(adv.getCreateDate().getTime());
                communityAdVo.setAdId(adv.getId());
                communityAdVo.setDevId(devInfoList.get(0).getId());
                list.add(communityAdVo);
            }
        }
        return list;
    }
}
